package com.xnyzc.lhy.order.controller.comment;


import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.param.comment.EvaluationParam;
import com.xnyzc.lhy.order.service.comment.IOaSysCommentService;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评价配置表 前端控制器
 * </p>
 *
 * @author zhuyubin
 * @since 2019-08-05
 */
@RestController
@RequestMapping("oa/comment")
public class OaSysCommentController {

    @Autowired
    private IOaSysCommentService iOaSysCommentService;

    @GetMapping(value = "getComment")
    public Rv getComment(){
        String userType = TokenUtil.getUserType();
        String tag = TokenUtil.getTag();
        if (StringUtils.isEmpty(userType) || StringUtils.isEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaSysCommentService.getComment(userType, tag));
    }

    /**
     * 评价提交
     * @param evaluationParam 入参实体
     * @return
     */
    @PostMapping(value = "evaluation")
    public Rv evaluation(@ApiParam(name = "evaluationParam", value = "入参实体") @RequestBody EvaluationParam evaluationParam){
        Long userid = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userid)){
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String userType = TokenUtil.getUserType();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(userType, tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        String orderNo = evaluationParam.getOrderNo();
        List<Long> comments = evaluationParam.getComments();
        if (CheckUtil.objIsEmpty(orderNo, comments)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iOaSysCommentService.evaluation(userid, Integer.valueOf(userType), tag, orderNo, comments), new HashMap<>(16));
    }
}
